<template>
  <div class="app-container">



    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="商品图片" prop="goodPicture">
        <el-input
          v-model="queryParams.goodPicture"
          placeholder="请输入商品图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="商品名称" prop="goodName">
        <el-input v-model="queryParams.goodName" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品分类" prop="goodClassify">
        <el-input v-model="queryParams.goodClassify" placeholder="请输入商品分类" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- 修改为下拉选择框，使用gsortId作为值 -->
      <el-form-item label="商品类型" prop="gsortId">
        <el-select v-model="queryParams.gsortId" placeholder="请选择商品类型" clearable>
          <el-option v-for="sort in goodSortList" :key="sort.gsortId" :label="sort.gsortName" :value="sort.gsortId" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="商品原价" prop="goodOriginalPrice">
        <el-input
          v-model="queryParams.goodOriginalPrice"
          placeholder="请输入商品原价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员价" prop="goodMemberPrice">
        <el-input
          v-model="queryParams.goodMemberPrice"
          placeholder="请输入商品会员价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="商品积分" prop="goodIntegral">
        <el-input v-model="queryParams.goodIntegral" placeholder="请输入商品积分" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="佣金比例" prop="goodCommissionRate">
        <el-input v-model="queryParams.goodCommissionRate" placeholder="请输入佣金比例" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="库存" prop="goodStock">
        <el-input v-model="queryParams.goodStock" placeholder="请输入库存" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="状态" prop="goodStatus">
        <el-select v-model="queryParams.goodStatus" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.sys_good_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="创建时间" prop="goodCreateTime">
        <el-date-picker clearable v-model="queryParams.goodCreateTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="goodUpdateTime">
        <el-date-picker clearable v-model="queryParams.goodUpdateTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="备用字段1" prop="demo1">
        <el-input
          v-model="queryParams.demo1"
          placeholder="请输入备用字段1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="备用字段2" prop="demo2">
        <el-input
          v-model="queryParams.demo2"
          placeholder="请输入备用字段2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备用字段3" prop="demo3">
        <el-input
          v-model="queryParams.demo3"
          placeholder="请输入备用字段3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="校区id" prop="campusId">
        <el-input v-model="queryParams.campusId" placeholder="请输入校区id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="是否分销" prop="isDistribution">
        <el-select v-model="queryParams.isDistribution" placeholder="请选择是否分销" clearable>
          <el-option v-for="dict in dict.type.sys_is_distribution" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="订单id" prop="orderId">
        <el-input v-model="queryParams.orderId" placeholder="请输入订单id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="商品排序号" prop="goodSort">
        <el-select v-model="queryParams.goodSort" placeholder="请选择商品排序号" clearable>
          <el-option
            v-for="dict in dict.type.sys_good_sort"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['artGood:artGood:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['artGood:artGood:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['artGood:artGood:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['artGood:artGood:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="artGoodList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="商品id" align="center" prop="goodId" /> -->
      <el-table-column label="商品名称" align="center" prop="goodName" width="180" fixed="left" />
      <el-table-column label="商品图片" align="center" width="120">
        <template slot-scope="scope">
          <div class="table-image-container">
            <el-image :src="scope.row.goodPicture" alt="商品图片" class="table-image" @error="handleImageError(scope.row)"
              :preview-src-list="[scope.row.goodPicture]" />
          </div>
        </template>
      </el-table-column>

      <el-table-column label="商品描述" align="center" prop="goodDesc" width="180" />
      <el-table-column label="商品分类" align="center" prop="goodClassify" width="140" />
      <el-table-column label="商品类型" align="center" prop="goodSort.gsortName" />
      <el-table-column label="商品原价" align="center" prop="goodOriginalPrice" />
      <el-table-column label="商品会员" align="center" prop="goodMemberPrice" />
      <el-table-column label="商品积分" align="center" prop="goodIntegral" />
      <el-table-column label="佣金比例" align="center" prop="goodCommissionRate" />
      <el-table-column label="库存" align="center" prop="goodStock" />
      <el-table-column label="状态" align="center" prop="goodStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_good_status" :value="scope.row.goodStatus" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="goodCreateTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.goodCreateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="goodUpdateTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.goodUpdateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备用字段1" align="center" prop="demo1" />
      <el-table-column label="备用字段2" align="center" prop="demo2" />
      <el-table-column label="备用字段3" align="center" prop="demo3" /> -->
      <el-table-column label="校区id" align="center" prop="campusId" />
      <el-table-column label="是否分销" align="center" prop="isDistribution">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_distribution" :value="scope.row.isDistribution" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="订单id" align="center" prop="orderId" /> -->
      <!-- <el-table-column label="商品排序号" align="center" prop="goodOrder">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_good_sort" :value="scope.row.goodOrder"/>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['artGood:artGood:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['artGood:artGood:remove']">删除</el-button>
          <el-button size="mini" type="text" v-hasPermi="['artGood:artGood:order']">订单</el-button>
          <el-button size="mini" type="success" icon="el-icon-s-operation" @click="handleDistribution(scope.row)">分销</el-button>
          <el-dropdown trigger="click">
            <el-button size="mini" type="text">排序<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleMove(scope.row, 'move_up')">
                <i class="el-icon-arrow-up"></i> 上移
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleMove(scope.row, 'move_down')">
                <i class="el-icon-arrow-down"></i> 下移
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商品表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="商品图片" prop="goodPicture">
          <el-input v-model="form.goodPicture" placeholder="请输入商品图片" />
        </el-form-item> -->
        <!-- 图片上传 -->
        <!-- <el-form-item label="商品图片" prop="goodPicture">
          <el-upload class="upload-demo" drag action :multiple="false" :http-request="uploadImg">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item> -->
        <el-form-item label="商品图片" prop="goodPicture">
          <el-upload class="upload-demo" drag action :multiple="false" :http-request="uploadImg"
            :show-file-list="false">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>

          <!-- 图片预览区域 -->
          <div v-if="form.goodPicture" class="image-preview">
            <img :src="form.goodPicture" alt="预览图" class="preview-image">
          </div>
        </el-form-item>

        <el-form-item label="商品名称" prop="goodName">
          <el-input v-model="form.goodName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品分类" prop="goodClassify">
          <el-input v-model="form.goodClassify" placeholder="请输入商品分类" />
        </el-form-item>
        <!-- <el-form-item label="商品类型" prop="gsortName">
          <el-input v-model="form.gsortName" placeholder="请输入商品类型" />
        </el-form-item> -->
        <!-- 修改商品类型查询为下拉选择框，使用gsortName -->
        <!-- <el-form-item label="商品类型" prop="gsortName">
          <el-select v-model="queryParams.gsortName" placeholder="请选择商品类型" clearable>
            <el-option v-for="sort in goodSortList" :key="sort.gsortId" :label="sort.gsortName"
              :value="sort.gsortName" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="商品类型" prop="gsortId">
          <el-select v-model="form.gsortId" placeholder="请选择商品类型" clearable>
            <el-option v-for="sort in goodSortList" :key="sort.gsortId" :label="sort.gsortName" :value="sort.gsortId" />
          </el-select>
        </el-form-item>
        <el-form-item label="商品描述" prop="goodDesc">
          <el-input v-model="form.goodDesc" placeholder="请输入商品描述" />
        </el-form-item>
        <el-form-item label="商品原价" prop="goodOriginalPrice">
          <el-input v-model="form.goodOriginalPrice" placeholder="请输入商品原价" />
        </el-form-item>
        <el-form-item label="商品会员" prop="goodMemberPrice">
          <el-input v-model="form.goodMemberPrice" placeholder="请输入商品会员" />
        </el-form-item>
        <el-form-item label="商品积分" prop="goodIntegral">
          <el-input v-model="form.goodIntegral" placeholder="请输入商品积分" />
        </el-form-item>
        <el-form-item label="佣金比例" prop="goodCommissionRate">
          <el-input v-model="form.goodCommissionRate" placeholder="请输入佣金比例" />
        </el-form-item>
        <el-form-item label="库存" prop="goodStock">
          <el-input v-model="form.goodStock" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="状态" prop="goodStatus">
          <el-select v-model="form.goodStatus" placeholder="请选择状态">
            <el-option v-for="dict in dict.type.sys_good_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="创建时间" prop="goodCreateTime">
          <el-date-picker clearable v-model="form.goodCreateTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="goodUpdateTime">
          <el-date-picker clearable v-model="form.goodUpdateTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="备用字段1" prop="demo1">
          <el-input v-model="form.demo1" placeholder="请输入备用字段1" />
        </el-form-item>
        <el-form-item label="备用字段2" prop="demo2">
          <el-input v-model="form.demo2" placeholder="请输入备用字段2" />
        </el-form-item>
        <el-form-item label="备用字段3" prop="demo3">
          <el-input v-model="form.demo3" placeholder="请输入备用字段3" />
        </el-form-item> -->
        <el-form-item label="校区id" prop="campusId">
          <el-input v-model="form.campusId" placeholder="请输入校区id" />
        </el-form-item>
        <el-form-item label="是否分销" prop="isDistribution">
          <el-select v-model="form.isDistribution" placeholder="请选择是否分销">
            <el-option v-for="dict in dict.type.sys_is_distribution" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item> -->
        <!-- <el-form-item label="商品排序号" prop="goodOrder">
          <el-select v-model="form.goodOrder" placeholder="请选择商品排序号">
            <el-option v-for="dict in dict.type.sys_good_sort" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="分销设置" :visible.sync="distributionDialogVisible" width="400px">
      <el-form>
        <el-form-item label="是否分销">
          <el-select v-model="distributionStatus" placeholder="请选择">
            <el-option label="分销" :value="0"></el-option>
            <el-option label="不分销" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="distributionDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmDistribution">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listArtGood, getArtGood, delArtGood, addArtGood, updateArtGood, moveUp, moveDown, uploadIdCard } from "@/api/artGood/artGood"
import { listGoodSort } from "@/api/art/goodSort"
import { parseTime } from '@/utils/artGood/date'

export default {
  name: "ArtGood",
  dicts: ['sys_good_status', 'sys_good_sort', 'sys_is_distribution'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表表格数据
      artGoodList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodPicture: null,
        goodName: null,
        goodClassify: null,
        goodDesc: null,
        gsortId: null,
        goodOriginalPrice: null,
        goodMemberPrice: null,
        goodIntegral: null,
        goodCommissionRate: null,
        goodStock: null,
        goodStatus: null,
        goodCreateTime: null,
        goodUpdateTime: null,
        demo1: null,
        demo2: null,
        demo3: null,
        campusId: null,
        isDistribution: null,
        orderId: null,
        goodOrder: null,
        // gsortName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        gsortId: [
          { required: true, message: "商品类型不能为空", trigger: "blur" }
        ],
      },
      goodSortList: [],       // 商品类型列表
      goodSortLoading: false, // 商品类型加载状态
      previewImage: '', // 图片预览URL
      distributionDialogVisible: false,
      distributionStatus: '',
      currentDistributionRow: null,
    }
  },
  created() {
    this.getList()
    this.loadGoodSortList() // 初始化加载商品类型列表
    // this.getGoodSortList() // 加载商品类型列表
  },
  methods: {
    /** 查询商品表列表 */
    getList() {
      // this.loading = true
      // listArtGood(this.queryParams).then(response => {
      //   this.artGoodList = response.rows
      //   this.total = response.total
      //   this.loading = false
      // })
      this.loading = true
      listArtGood(this.queryParams).then(response => {
        this.artGoodList = response.rows
        // 格式化时间字段
        this.artGoodList.forEach(item => {
          // 只格式化有值的字段
          if (item.goodCreateTime) {
            item.goodCreateTime = this.formatTime(item.goodCreateTime)
          }
          if (item.goodUpdateTime) {
            item.goodUpdateTime = this.formatTime(item.goodUpdateTime)
          }
        })
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        goodId: null,
        goodPicture: null,
        goodName: null,
        goodClassify: null,
        goodDesc: null,
        gsortId: null,
        goodOriginalPrice: null,
        goodMemberPrice: null,
        goodIntegral: null,
        goodCommissionRate: null,
        goodStock: null,
        goodStatus: null,
        goodCreateTime: null,
        goodUpdateTime: null,
        demo1: null,
        demo2: null,
        demo3: null,
        campusId: null,
        isDistribution: null,
        orderId: null,
        goodOrder: null,
        gsortName: null,
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log('查询参数:', this.queryParams); // 添加日志
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加商品表"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const goodId = row.goodId || this.ids
      getArtGood(goodId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改商品表"
      })
    },
    uploadImg(item) {
      const formData = new FormData();
      formData.append("file", item.file);

      uploadIdCard(formData)
        .then(res => {
          this.$modal.msgSuccess("上传成功");
          // 直接赋值给表单的goodPicture字段
          this.form.goodPicture = res.data.file.url;
          console.log('图片URL已保存到表单:', this.form.goodPicture);

          // 可选：上传成功后显示预览
          this.previewImage = res.data.file.url;
        })
        .catch(error => {
          this.$modal.msgError("上传失败: " + error.message);
          console.error('图片上传失败:', error);
        });
    },
    /** 提交按钮 */
    submitForm() {
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.goodId != null) {
      //       updateArtGood(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功")
      //         this.open = false
      //         this.getList()
      //       })
      //     } else {
      //       addArtGood(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功")
      //         this.open = false
      //         this.getList()
      //       })
      //     }
      //   }
      // })
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodId != null) {
            updateArtGood(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addArtGood(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodIds = row.goodId || this.ids
      this.$modal.confirm('是否确认删除商品表编号为"' + goodIds + '"的数据项？').then(function () {
        return delArtGood(goodIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('artGood/artGood/export', {
        ...this.queryParams
      }, `artGood_${new Date().getTime()}.xlsx`)
    },

    handleMove(row, direction) {
      const goodId = row.goodId;

      // 先判断是否是第一条或最后一条（前端预检查）
      const allOrders = this.artGoodList.map(item => item.goodOrder);
      const currentOrder = row.goodOrder;
      const isFirst = currentOrder === Math.min(...allOrders);
      const isLast = currentOrder === Math.max(...allOrders);

      if (direction === 'move_up') {
        if (isFirst) {
          this.$message.warning('已经是第一条，无法上移');
          return;
        }
        moveUp(goodId).then(response => {
          if (response.code === 200) {
            this.$modal.msgSuccess('上移成功');
            this.getList();
          } else {
            this.$message.error('上移失败，请稍后再试');
          }
        });
      } else {
        if (isLast) {
          this.$message.warning('已经是最后一条，无法下移');
          return;
        }
        moveDown(goodId).then(response => {
          if (response.code === 200) {
            this.$modal.msgSuccess('下移成功');
            this.getList();
          } else {
            this.$message.error('下移失败，请稍后再试');
          }
        });
      }
    },
    /** 加载商品类型列表 */
    loadGoodSortList() {
      listGoodSort().then(response => {
        this.goodSortList = response.rows
        console.log("让我看看")
        console.log(this.goodSortList)
      })
    },

    /** 搜索商品类型（下拉框远程搜索） */
    listGoodSort(query) {
      if (!query) {
        this.loadGoodSortList()
        return
      }

      this.goodSortLoading = true
      const params = { gsortName: query }
      listGoodSort(params).then(response => {
        this.goodSortList = response.data
        this.goodSortLoading = false
      })
    },
    // 时间格式化
    formatTime(date) {
      // 确保格式与后端一致: yyyy-MM-dd HH:mm:ss
      return parseTime(date, '{y}-{m}-{d} {h}:{i}:{s}')
    },
    // 处理图片加载错误
    handleImageError(row) {
      console.log('图片加载失败:', row.goodPicture);
      // 可以在这里设置默认图片
      // row.goodPicture = '/default-image.jpg';
    },
    handleDistribution(row) {
      this.currentDistributionRow = row;
      this.distributionStatus = row.isDistribution == null ? '' : row.isDistribution;
      this.distributionDialogVisible = true;
    },
    confirmDistribution() {
      // 只更新isDistribution和goodId
      const updateData = {
        goodId: this.currentDistributionRow.goodId,
        isDistribution: this.distributionStatus
      };
      // 直接调用updateArtGood方法
      updateArtGood(updateData).then(response => {
        this.$modal.msgSuccess('修改成功');
        this.getList();
        this.distributionDialogVisible = false;
      });
    },
  }
}
</script>

<style scoped>
.horizontal-dropdown .el-dropdown-menu {
  display: flex;
}

.el-table .el-button {
  font-size: 13px;
  color: rgb(81, 80, 80);
  border: 1px solid rgb(203, 202, 202);
  background-color: white;
  padding: 8px 5px;
}

/* 预览图 */
.preview-image {
  height: 100px;
  width: 100px;
}

.table-image-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.table-image {
  max-width: 80px;
  max-height: 80px;
  border-radius: 4px;
  object-fit: cover;
  border: 1px solid #ebeef5;
}
</style>
